package main

import (
	"fmt"
	"net"
	"net/http"
	"reflect"
	"time"
)

func main() {
	user := User{name: "sam", id: 1}
	t := reflect.TypeOf(user)
	v := reflect.ValueOf(&user)

	// fmt.Println("v:", v.NumMethod())
	// methods := v.NumMethod()
	// index := 0
	// for index < methods {
	// 	method := v.Method(index)
	// 	args := []reflect.Value{}
	// 	method.Call(args)
	// 	index++
	// }

	at := reflect.TypeOf(&user)

	numMethods := at.NumMethod()
	i := 0
	for i < numMethods {
		method := at.Method(i)
		fmt.Println(method.Name, method.PkgPath, method.Type, method.Func)
		i++
	}

	nums := t.NumMethod()
	i = 0
	for i < nums {
		method := at.Method(i)
		fmt.Println(method.Name, method.PkgPath, method.Type, method.Func)
		i++
	}

	numFields := t.NumField()
	i = 0
	for i < numFields {
		field := t.Field(i)
		fmt.Println(field.Name, field.Type)
		i++

	}
	fmt.Println("type:", t, t.NumMethod(), at, at.NumMethod(), v)

	ex := make(chan byte)

	go func(ch <-chan byte) {
		for {
			select {
			case <-ch:
				{
					println("go1")
					return
				}
			}
		}
	}(ex)

	go func(ch <-chan byte) {
		for {
			select {
			case <-ch:
				{
					println("go2")
					return
				}
			}
		}
	}(ex)

	close(ex)

	slice := make([]int, 2)

	slice[0] = 1
	slice[1] = 2
	slice = append(slice, 3)
	slice = append(slice, 4)
	fmt.Println("slice:", slice)

	server := &http.Server{
		Addr:           ":9090",
		Handler:        &User{},
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	server.ListenAndServe()
	net.Listen("tcp", ":8080")
}

// Index handlefunc
func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "hello")
	w.Write([]byte("word\r\n\r\n"))
}

// User 用户
type User struct {
	name string
	id   int
}

func (u *User) samtest() {
	fmt.Println("samtest")
}

// Test 测试
func (u *User) Test() {
	fmt.Println("Test")
}

// TestUser 测试
func (u User) TestUser() {
	fmt.Println("TestUser")
}

func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(r.RequestURI))
}
